#include <QtGui>
#include <memory.h>
#include "detaildlg.h"
#include "../core/aprcfg.h"

DetailDlg::DetailDlg(const QString& itemNameSuffix, det_u* detail, EsdItemMap* map, QWidget* parent/* = 0*/)
	: QDialog(parent)
{
	if(itemNameSuffix.contains(tr("DLM"))){
	//	bDlmItem = true;
		detailType = 1;
	}else if(itemNameSuffix.contains(tr("DCC"))){
	//	bDlmItem = false;
		detailType = 2;
	}else{
		detailType = 0;
	}
	
	buttonBox = new QDialogButtonBox;
	QPushButton* btnOk =
		 buttonBox->addButton(tr("OK"), QDialogButtonBox::ActionRole);
	buttonBox->addButton(tr("CANCEL"), QDialogButtonBox::RejectRole);
	
	if(2 == detailType){
		// ESD演算项目
		esdMap = map;

		QLabel* lblRefItem1 = new QLabel(tr("REF.ITEM1"));
		QLabel* lblRefItem2 = new QLabel(tr("REF.ITEM2"));

		QStringList itemList;
		int index = 0;
		while(map[index].index != -1){
//			qDebug() << map[index].index;
			if(itemNameSuffix != map[index].itemNameSuffix){
				// 本item自身不要包含在ref item列表中
				itemList << map[index].itemNameSuffix;
			}
			++index;
	//		esdChList[index] = map[index++].ch;
		}
		cmbRefItem1 = new QComboBox;
		cmbRefItem2 = new QComboBox;
		cmbRefItem1->addItems(itemList);
		cmbRefItem2->addItems(itemList);

		QGridLayout* esdLayout = new QGridLayout;
		esdLayout->addWidget(lblRefItem1, 0, 0);
		esdLayout->addWidget(cmbRefItem1, 0, 1);
		esdLayout->addWidget(lblRefItem2, 1, 0);
		esdLayout->addWidget(cmbRefItem2, 1, 1);
		esdLayout->addWidget(buttonBox, 2, 0, 1, 2);
		setLayout(esdLayout);

	}else{
		// HUE,VL,DLM项目

		QLabel* lblAutoCalib	= new QLabel(tr("AUTO CALIB"));
		QLabel* lblOfsCancel	= new QLabel(tr("OFFSET CANCEL"));
		QLabel* lblMeasRange1	= new QLabel(tr("MEAS RANGE1"));
		QLabel* lblGain1		= new QLabel(tr("GAIN1"));
		QLabel* lblMeasRange2	= new QLabel(tr("MEAS RANGE2"));
		QLabel* lblGain2		= new QLabel(tr("GAIN2"));
		QLabel* lblMeasRange3	= new QLabel(tr("MEAS RANGE3"));
		QLabel* lblGain3		= new QLabel(tr("GAIN3"));
	
		QStringList listOnOff;
		AprCfg::getNamesByAttr(PM_ONOFF_ATTR, listOnOff);
		cmbAutoCalib = new QComboBox;
		cmbAutoCalib->addItems(listOnOff);
		cmbOfsCancel = new QComboBox;
		cmbOfsCancel->addItems(listOnOff);
	
		QStringList listHueRanges;
		AprCfg::getNamesByAttr(PM_HUERNG_ATTR, listHueRanges);
		cmbMeasRange1 = new QComboBox;
		cmbMeasRange2 = new QComboBox;
		cmbMeasRange3 = new QComboBox;
		cmbMeasRange1->addItems(listHueRanges);
		cmbMeasRange2->addItems(listHueRanges);
		cmbMeasRange3->addItems(listHueRanges);
	
		QStringList listHueScfs;
		AprCfg::getNamesByAttr(PM_HUESCF_ATTR, listHueScfs);
		cmbGain1 = new QComboBox;
		cmbGain2 = new QComboBox;
		cmbGain3 = new QComboBox;
		cmbGain1->addItems(listHueScfs);	
		cmbGain2->addItems(listHueScfs);	
		cmbGain3->addItems(listHueScfs);	
	
		QGridLayout* glayout = new QGridLayout;
		glayout->addWidget(lblAutoCalib, 0, 0);
		glayout->addWidget(cmbAutoCalib, 0, 1);
		glayout->addWidget(lblOfsCancel, 1, 0);
		glayout->addWidget(cmbOfsCancel, 1, 1);
		glayout->addWidget(lblMeasRange1, 2, 0);
		glayout->addWidget(cmbMeasRange1, 2, 1);
		glayout->addWidget(lblGain1, 3, 0);
		glayout->addWidget(cmbGain1, 3, 1);
		glayout->addWidget(lblMeasRange2, 4, 0);
		glayout->addWidget(cmbMeasRange2, 4, 1);
		glayout->addWidget(lblGain2, 5, 0);
		glayout->addWidget(cmbGain2, 5, 1);
		glayout->addWidget(lblMeasRange3, 6, 0);
		glayout->addWidget(cmbMeasRange3, 6, 1);
		glayout->addWidget(lblGain3, 7, 0);
		glayout->addWidget(cmbGain3, 7, 1);
		glayout->addWidget(buttonBox, 8, 0, 1, 2);
		setLayout(glayout);
	
	//	if(!bDlmItem){
		if(0 == detailType){
			lblMeasRange3->setVisible(false);
			cmbMeasRange3->setVisible(false);
			lblGain3->setVisible(false);
			cmbGain3->setVisible(false);
		}
	}

	memcpy(&hueDetail, detail, sizeof(det_u));
	initDetails();	
	
	QString title = tr("DETAIL SETTING - ") + itemNameSuffix;
	setWindowTitle(title);

	connect(btnOk, SIGNAL(clicked()), this, SLOT(saveDetailSetting()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

}

void DetailDlg::initDetails()
{
	//
	// TODO: 这里不应该靠判断autorange的值来判断是否设置过,dcc项目未含入
	//
	if(-1 == hueDetail.hue.autorange){
		// 尚未设置过该item的hue detail
		qDebug() << "////////// -1";
		return;
	}

	if(2 == detailType){
		bool found = false;
		short ino = hueDetail.esd[0].ino;
	//	qDebug() << "===========ino of esd 0 - " << ino;
		QString nameSuffix;
		int indexCmb = 0;
//		QString name;
		int i = 0;
		while(esdMap[i].index != -1){
			if(ino == esdMap[i].index){
				nameSuffix = esdMap[i].itemNameSuffix;
//				name = nameSuffix.left(nameSuffix.indexOf('_'));
				found = true;
				break;
			}
			++i;
		}
		if(found){
			indexCmb = cmbRefItem1->findText(nameSuffix);
		}
		cmbRefItem1->setCurrentIndex(indexCmb);

		ino = hueDetail.esd[1].ino;
		i = 0;
		while(esdMap[i].index != -1){
			if(ino == esdMap[i].index){
				nameSuffix = esdMap[i].itemNameSuffix;
//				name = nameSuffix.left(nameSuffix.indexOf('_'));
				break;
			}
			++i;
		}
		if(found){
			indexCmb = cmbRefItem2->findText(nameSuffix);
		}
		cmbRefItem2->setCurrentIndex(indexCmb);

	}else{
	
		QString strOption;
		int index = 0;
		
		AprCfg::getParamNameByCode(hueDetail.hue.autorange, strOption);
		index = cmbAutoCalib->findText(strOption);
		cmbAutoCalib->setCurrentIndex(index);
		
		AprCfg::getParamNameByCode(hueDetail.hue.ofstcancel, strOption);
		index = cmbOfsCancel->findText(strOption);
		cmbOfsCancel->setCurrentIndex(index);
		
		AprCfg::getParamNameByCode(hueDetail.hue.mrng1, strOption);
		index = cmbMeasRange1->findText(strOption);
		cmbMeasRange1->setCurrentIndex(index);
		
		AprCfg::getParamNameByCode(hueDetail.hue.scf1, strOption);
		index = cmbGain1->findText(strOption);
		cmbGain1->setCurrentIndex(index);
		
		AprCfg::getParamNameByCode(hueDetail.hue.mrng2, strOption);
		index = cmbMeasRange2->findText(strOption);
		cmbMeasRange2->setCurrentIndex(index);
		
		AprCfg::getParamNameByCode(hueDetail.hue.scf2, strOption);
		index = cmbGain2->findText(strOption);
		cmbGain2->setCurrentIndex(index);
	
		if(0 == detailType){	
			AprCfg::getParamNameByCode(hueDetail.hue.mrng3, strOption);
			index = cmbMeasRange3->findText(strOption);
			cmbMeasRange3->setCurrentIndex(index);
			
			AprCfg::getParamNameByCode(hueDetail.hue.scf3, strOption);
			index = cmbGain3->findText(strOption);
			cmbGain3->setCurrentIndex(index);
		}
	}
}

void DetailDlg::saveDetailSetting()
{
	if(2 == detailType){
		int index1 = cmbRefItem1->currentIndex();
		int index2 = cmbRefItem2->currentIndex();
		QString itemNameSuffix;
		QString itemName;
		short	suffix = 0;
		ushort	code = 0;

		hueDetail.esd[0].ino = esdMap[index1].index;
		hueDetail.esd[0].ch  = esdMap[index1].ch;
		itemNameSuffix = esdMap[index1].itemNameSuffix;
		itemName = itemNameSuffix.left(itemNameSuffix.indexOf('_'));
		suffix = itemNameSuffix.at(itemNameSuffix.indexOf('_')+1).toAscii();
		AprCfg::getParamCodeByName(itemName, PM_ITEM_ATTR, code);
		hueDetail.esd[0].code = code;
		hueDetail.esd[0].suffix  = suffix;

		hueDetail.esd[1].ino = esdMap[index2].index;
		hueDetail.esd[1].ch  = esdMap[index2].ch;
		itemNameSuffix = esdMap[index2].itemNameSuffix;
		itemName = itemNameSuffix.left(itemNameSuffix.indexOf('_'));
		suffix = itemNameSuffix.at(itemNameSuffix.indexOf('_')+1).toAscii();
		AprCfg::getParamCodeByName(itemName, PM_ITEM_ATTR, code);
		hueDetail.esd[1].code = code;
		hueDetail.esd[1].suffix  = suffix;

		qDebug() << "dcc detail!!!!!!!!!!!!!!";
		qDebug() << hueDetail.esd[0].ino;
		qDebug() << hueDetail.esd[0].code;
		qDebug() << hueDetail.esd[0].suffix;
		qDebug() << hueDetail.esd[0].ch;
		qDebug() << hueDetail.esd[1].ino;
		qDebug() << hueDetail.esd[1].code;
		qDebug() << hueDetail.esd[1].suffix;
		qDebug() << hueDetail.esd[1].ch;

	}else{

		ushort autoCode 	= 0;
		ushort ofsCode		= 0;
		ushort rngCode1		= 0;
		ushort gainCode1	= 0;
		ushort rngCode2		= 0;
		ushort gainCode2	= 0;
		ushort rngCode3		= 0;
		ushort gainCode3	= 0;
		QString paramName;
		
		paramName = cmbAutoCalib->currentText();
		AprCfg::getParamCodeByName(paramName, PM_ONOFF_ATTR, autoCode);
	
		paramName = cmbOfsCancel->currentText();
		AprCfg::getParamCodeByName(paramName, PM_ONOFF_ATTR, ofsCode);
	
		paramName = cmbMeasRange1->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUERNG_ATTR, rngCode1);
		
		paramName = cmbGain1->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUESCF_ATTR, gainCode1);
	
		paramName = cmbMeasRange2->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUERNG_ATTR, rngCode2);
		
		paramName = cmbGain2->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUESCF_ATTR, gainCode2);
	
		paramName = cmbMeasRange3->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUERNG_ATTR, rngCode3);
		
		paramName = cmbGain3->currentText();
		AprCfg::getParamCodeByName(paramName, PM_HUESCF_ATTR, gainCode3);
	
		hueDetail.hue.autorange = autoCode;
		hueDetail.hue.ofstcancel= ofsCode;
		hueDetail.hue.mrng1		= rngCode1;
		hueDetail.hue.scf1		= gainCode1;
		hueDetail.hue.mrng2		= rngCode2;
		hueDetail.hue.scf2		= gainCode2;
		hueDetail.hue.mrng3		= rngCode3;
		hueDetail.hue.scf3		= gainCode3;	
	}
/*
	qDebug() << "============== hue ======================";
	qDebug() << hueDetail.autorange;
	qDebug() << hueDetail.ofstcancel;
	qDebug() << hueDetail.mrng1;
	qDebug() << hueDetail.scf1;
	qDebug() << hueDetail.mrng2;
	qDebug() << hueDetail.scf2;
	qDebug() << hueDetail.mrng3;
	qDebug() << hueDetail.scf3;
*/
	
	accept();
}


//
// 将设置后的det_u拷贝到指定的区域
//
void DetailDlg::getHueDetail(det_u* detail)
{
	//qDebug() << "getHueDetail triggled!!!";
	memcpy(detail, &hueDetail, sizeof(det_u));	
}
